package com.fs.console.base.controller;

import com.fs.common.baseMapper.UserMapper;
import com.fs.common.pojo.User;
import com.fs.console.base.enums.CodeEnum;
import com.fs.console.base.interceptor.AuthenticationInterceptor;
import com.fs.console.base.utils.ApplicationUtils;
import com.fs.console.base.utils.EncodeUtil;
import com.fs.console.base.utils.StringToolUtils;
import com.fs.console.base.vo.JsonResponse;
import com.fs.console.base.vo.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户controller
 */
@Slf4j
@Controller
@RequestMapping("/console/user/rewrite")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/firstUpdatePwd")
    public JsonResponse firstUpdatePwd(){
        try {
            String pwd = StringToolUtils.SystemGetString("pwd");
            LoginUser loginUser = ApplicationUtils.getUser();
            User user = loginUser.getUser();
            if(StringUtils.isBlank(pwd)){
                return JsonResponse.error("密码不能为空");
            }
            User user1 = userMapper.selectById(user.getId());
            user1.setPwd(EncodeUtil.getSignAndMD5(pwd));
            user1.setIsUpdatePwd(0);
            userMapper.updateById(user1);
            loginUser.setUser(user1);
            AuthenticationInterceptor.loginUserMap.put(user.getId(),loginUser);
            JsonResponse success = JsonResponse.success(user1);
            success.setMsg("修改密码成功");
            return success;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

}
